package efr.dao.impl;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Session;

import efr.dao.ComentariuDao;
import efr.entity.Comentariu;
import efr.util.HibernateUtil;

public class ComentariuDaoImpl implements ComentariuDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Comentariu> getAllComentarii() {
		List<Comentariu> listComentarii = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		listComentarii = session.createQuery("FROM Comentariu C WHERE C.status='N' ORDER BY C.id DESC").list();
		for (Comentariu comentariu : listComentarii) {
			Hibernate.initialize(comentariu.getPoezie());
		}
		session.getTransaction().commit();
		return listComentarii;
	}

	@Override
	public Comentariu getComentariu(int id) {
		Comentariu comentariu = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		comentariu = (Comentariu)session.get(Comentariu.class, id);
		Hibernate.initialize(comentariu.getPoezie());
		session.getTransaction().commit();
		return comentariu;
	}

	@Override
	public void updateComentariu(Comentariu comentariu) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.update(comentariu);
		session.getTransaction().commit();
		
	}

	@Override
	public Integer addComentariu(Comentariu comentariu) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Integer id = (Integer)session.save(comentariu);
		session.getTransaction().commit();
		return id;
	}

	@Override
	public void deleteComentariu(Comentariu comentariu) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.delete(comentariu);
		session.getTransaction().commit();
	}
}
